package xyz.kuailemao.mvc.handler;

import com.alibaba.fastjson.JSONObject;
import entity.ParamData;
import entity.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.kuailemao.service.api.AdminService;
import entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import xyz.kuailemao.util.CrowdUtil;
import xyz.kuailemao.util.ResultEntity;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class TestHandler {

    @Autowired
    private AdminService adminService;

    Logger logger = LoggerFactory.getLogger(TestHandler.class);

    @ResponseBody
    @RequestMapping("/send/compose/object.json")
    public String saveReceiveComposeObject(@RequestBody String str, HttpServletRequest request){
        //  判断是否是ajax请求
        boolean judgeResult= CrowdUtil.judgeRequestType(request);
        logger.info("judgeResult=="+judgeResult);

        logger.info("一"+"judgeResult="+judgeResult);
        Student student= JSONObject.parseObject(str, Student.class);
        logger.info("二"+student.toString());

        // 将"查询"到的Student对象封装到ResultEntity中返回
        ResultEntity<Student> resultEntity= ResultEntity.successWithData(student);
        logger.info("三"+resultEntity.toString());
        // 转换为 JSON 格式
        logger.info("四"+JSONObject.toJSON(resultEntity).toString());

        return JSONObject.toJSON(resultEntity).toString();
    }

    @ResponseBody
    @RequestMapping("/send/array/one.html")
    public String testReceiveArrayOne(@RequestParam("array[]") List<Integer> array) {
        for (Integer number : array) {
            System.out.println("number====" + number);
        }
        return "success";
    }

    @ResponseBody
    @RequestMapping("/send/array/two.html")
    public String testReceiveArrayTwo(ParamData paramData) {
        List<Integer> array = paramData.getArray();

        for (Integer number : array) {
            System.out.println("number====" + number);
        }
        return "success";
    }

/*

    @ResponseBody
    @RequestMapping("/send/array/three.html")
    public String testReceiveArrayThree(@RequestBody String str) {
        ParamData paramData = JSONObject.parseObject(str, ParamData.class);
        List<Integer> array = paramData.getArray();
        for (Integer number : array) {
            logger.info("number====" + number);
        }
        return "success";
    }
*/

    @ResponseBody
    @RequestMapping("/send/array/three.html")
    public String testReceiveArrayThree(@RequestBody List<Integer> array) {

        for (Integer number : array) {
            logger.info("number====" + number);
        }
        return "success";
    }

    // 老师的方法，不知道什么原因在我这里 @ResponseBody 失效了
    @ResponseBody
    @RequestMapping("/send/compose/object.html")
    public String testReceiveComposeObject(@RequestBody Student student){
        logger.info(student.toString());
        return "success";
    }



    @RequestMapping("/test/ssm.html")
    public String testSsm(ModelMap modelMap,HttpServletRequest request) {
        boolean judgeResult = CrowdUtil.judgeRequestType(request);
        logger.info("judgeResult=="+judgeResult);
        List<Admin> adminList = adminService.getAll();
        modelMap.addAttribute("adminList", adminList);

        // 制作空指针
        /*String a = null;
        System.out.println(a.length());*/

        // 数学异常
        System.out.println(10/0);
        return "target";
    }

}
